<script>

function addScriptToTab(tab){
	chrome.extension.sendRequest("gofhjkjmkpinhpoiabjplobcaignabnl",{method : 'stop-temporarily',tabId:tab.id});
	//chrome.tabs.executeScript(tab.id,{file:'jquery.js'},function(){});
	chrome.tabs.executeScript(tab.id,{code:"var lang = '"+localStorage["lang"]+"';var skipSettings = "+ (tab.url.lastIndexOf("#googtrans/auto/") != -1) +";"},function(){});
	chrome.tabs.executeScript(tab.id,{file:'executeScript.js'},function(){});
}

if (!chrome.pageAction.onClicked.hasListener(addScriptToTab)){
  chrome.pageAction.onClicked.addListener(addScriptToTab);
}

// true if tab has just changed it's url
//var freshUrlMap = [];

function isLegalTab(tab){
	return (tab.url.lastIndexOf("https://chrome.google.com/extensions") != 0 && tab.url.lastIndexOf("chrome://") != 0 && tab.url.lastIndexOf("chrome-extension://") != 0 && tab.url.lastIndexOf("about:") != 0 && tab.url.lastIndexOf("file://") != 0);
} 

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
	/*
	console.log(changeInfo.status,changeInfo.url);
	
	if (changeInfo.status == "loading" && changeInfo.url != "undefined"){
		freshUrlMap[tabId] = true;
	}
	else if (freshUrlMap[tabId] && changeInfo.status == "complete"){
		freshUrlMap[tabId] = false;
		//(tab.url.lastIndexOf("#googtrans/auto/") != -1) ? addScriptToTab(tab) : null;
	}
	*/
	if (changeInfo.status == "complete" && isLegalTab(tab)){
		chrome.pageAction.show(tab.id);
	}

});

// populate all tabs with my page action at extention start
chrome.windows.getAll({populate:true},function(windows){
	for (w in windows){
		for (t in windows[w].tabs){
			(isLegalTab(windows[w].tabs[t]))?chrome.pageAction.show(windows[w].tabs[t].id):null;
		}
	}	
	//console.log(windows);
});

//pay attention to setting saving
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){  //  <-- HERE
	console.log("save lang:",request.lang);
	localStorage["lang"] = request.lang;
})

</script>
